<template>
    <div class="system-file-container layout-padding">
        <el-card shadow="hover" class="layout-padding-auto">
            <div class="system-user-search mb15">
                <el-input v-auth="'toolsFile:query'" size="default" v-model="state.tableData.param.name" placeholder="请输入文件名称" style="max-width: 180px"> </el-input>
                <el-button v-auth="'toolsFile:query'" size="default" @click="getTableData" type="primary" class="ml10">
                    <el-icon>
                        <ele-Search />
                    </el-icon>
                    查询
                </el-button>
                <el-button v-auth="'toolsFile:add'" size="default" type="success" class="ml10" @click="onOpenAddFile('add')">
                    <el-icon>
                        <ele-FolderAdd />
                    </el-icon>
                    上传文件
                </el-button>
            </div>
            <el-table :data="state.tableData.data" v-loading="state.tableData.loading" style="width: 100%">
                <el-table-column type="index" label="序号" width="60" />
                <el-table-column prop="filename" label="文件名" show-overflow-tooltip></el-table-column>
                <el-table-column prop="content_type" label="文件类型" show-overflow-tooltip></el-table-column>
                <el-table-column prop="description" label="文件描述" show-overflow-tooltip></el-table-column>
                <el-table-column prop="upload_time" label="上传时间" show-overflow-tooltip></el-table-column>
                <el-table-column label="缩略图" show-overflow-tooltip>
                    <template v-slot="scope">
                            <el-image ref="imagePreview" :style="{ width: `40px`, height: `40px` }"
                                :src="'http://resource.shuonihao.cn/'+scope.row.path" :zoom-rate="1.2" :preview-src-list="previewList"
                                preview-teleported fit="cover" :initial-index="scope.$index"/>
                    </template>
                </el-table-column>
                <el-table-column prop="status" label="文件状态" show-overflow-tooltip>
                    <template #default="scope">
                        <el-tag type="success" v-if="scope.row.status === 0">启用</el-tag>
                        <el-tag type="info" v-else>禁用</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作" v-auths="['toolsFile:update','toolsFile:delete']" width="130">
                    <template #default="scope">
                        <el-button v-auth="'toolsFile:update'" size="small" text type="primary" @click="onOpenEditFile('edit', scope.row)">修改</el-button>
                        <el-button v-auth="'toolsFile:delete'" size="small" text type="primary" @click="onRowDel(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination @size-change="onHandleSizeChange" @current-change="onHandleCurrentChange" class="mt15"
                :pager-count="5" :page-sizes="[10, 20, 30]" v-model:current-page="state.tableData.param.page" background
                v-model:page-size="state.tableData.param.pageSize" layout="total, sizes, prev, pager, next, jumper"
                :total="state.tableData.total">
            </el-pagination>
        </el-card>
        <FileDialog ref="fileDialogRef" @refresh="getTableData()" />
    </div>
</template>

<script setup lang="ts" name="systemFile">
import { defineAsyncComponent, reactive, onMounted, ref } from 'vue';
import { ElMessageBox, ElMessage ,ElImage} from 'element-plus';
import { useFileApi } from '/@/api/file';

const fileApi = useFileApi()
// 引入组件
const FileDialog = defineAsyncComponent(() => import('/@/views/tool/file/dialog.vue'));

// 定义变量内容
const fileDialogRef = ref();
const state = reactive<SysFileState>({
    tableData: {
        data: [],
        total: 0,
        loading: false,
        param: {
            page: 1,
            pageSize: 10,
            name: ""
        },
    },
});
const previewList = ref<string[]>([])
// 重置
const previewReset=()=>{
    previewList.value = [] as string[];
    state.tableData.data.map(v=>previewList.value.push("http://resource.shuonihao.cn/"+v.path));
}

// 初始化表格数据
const getTableData = () => {
    state.tableData.loading = true;
    fileApi.getFiles(state.tableData.param).then(res => {
        state.tableData.data = res.result;
        state.tableData.total = res.total;
        previewReset();
    })
    setTimeout(() => {
        state.tableData.loading = false;
    }, 200);
};
// 打开新增字典弹窗
const onOpenAddFile = (type: string) => {
    fileDialogRef.value.openDialog(type);
};
// 打开修改字典弹窗
const onOpenEditFile = (type: string, row: RowDicType) => {
    fileDialogRef.value.openDialog(type, row);
};
// 删除字典
const onRowDel = (row: RowFileType) => {
    ElMessageBox.confirm(`此操作将永久删除文件：“${row.filename}”，是否继续?`, '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
    })
        .then(async() => {
            await fileApi.deleteFiles({ids:[row.id]}).then(res=>{
                getTableData();
                ElMessage.success('删除成功');
            })
        })
        .catch(() => { });
};
// 分页改变
const onHandleSizeChange = (val: number) => {
    state.tableData.param.pageSize = val;
    getTableData();
};
// 分页改变
const onHandleCurrentChange = (val: number) => {
    state.tableData.param.page = val;
    getTableData();
};
// 页面加载时
onMounted(() => {
    getTableData();
});
</script>
<style lang="scss" scoped>
.system-file-container {
    :deep(.el-card__body) {
        display: flex;
        flex-direction: column;
        flex: 1;
        overflow: auto;

        .el-table {
            flex: 1;
        }
    }
}
</style>